#ifndef _HARTCMD0_H_
#define _HARTCMD0_H_

/* Includes ------------------------------------------------------------------*/
#include "fm33lg0xx_fl.h"
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#ifdef _HARTCMD0_C_
#include "DataFlashMap.h"
#endif
/* USER CODE END Includes */

/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
typedef enum
{
	eHART_CMD0 = 0x00,
	eHART_CMD1,
	eHART_CMD2,
	eHART_CMD3,
	eHART_CMD4,
	eHART_CMD5,
	eHART_CMD6,
	eHART_CMD7,
	eHART_CMD8,
	eHART_CMD9,
	eHART_CMD11,
	eHART_CMD12,
	eHART_CMD13,
	eHART_CMD14,
	eHART_CMD15,
	eHART_CMD16,
	eHART_CMD17,
	eHART_CMD18,
	eHART_CMD19,
	eHART_CMD20,
	eHART_CMD21,
	eHART_CMD22,
	eHART_CMD34,
	eHART_CMD38,
	eHART_CMD48,
	
	eHART_CMD_MAX,
}EnumHartCmdId;

// enum Table
// Universal Commands
// Table2.Engineering Unit Codes
typedef enum
{
	// Pressure
	ePRESSURE_INCHES_OF_WATER_AT_68DEGREES_F = 1,
	ePRESSURE_INCHES_OF_MERCURY_AT_0DEGREES_C = 2,
	ePRESSURE_FEET_OF_WATER_AT_68DEGREES_F = 3,
	ePRESSURE_MILLIMETERS_OF_WATER_AT_68DEGREES_F = 4,
	ePRESSURE_MILLIMETERS_OF_MERCURY_AT_0DEGREES_C = 5,
	ePRESSURE_POUNDS_PER_SQUARE_INCH = 6,
	ePRESSURE_BARS = 7,
	ePRESSURE_MILLIBARS = 8,
	ePRESSURE_GRAMS_PER_SQUARE_CENTIMETER = 9,
	ePRESSURE_KILOGRAMS_PER_SQUARE_CENTIMETER = 10,
	ePRESSURE_PASCALS = 11,
	ePRESSURE_KILOPASCALS = 12,
	ePRESSURE_TORR = 13,
	ePRESSURE_ATMOSPHERES = 14,
	ePRESSURE_INCHES_OF_WATER_AT_60DEGREES_F = 145,
	ePRESSURE_MEGAPASCALS = 237,
	ePRESSURE_INCHES_OF_WATER_AT_4DEGREES_C = 238,
	ePRESSURE_MILLIMETERS_OF_WATER_AT_4DEGREES_C = 239,
	// Electrostatic Unit of Current
	eCurrent_Milliamperes = 39,
}EnumEngineeringUnitCode;
// Table3.Transfer Function Codes
typedef enum
{
	eTRANSFER_FUNC_LINEAR = 0,
	eTRANSFER_FUNC_SQUARE_ROOT = 1,
	eTRANSFER_FUNC_SQUARE_ROOT_THIRD_POWER = 2,
	eTRANSFER_FUNC_SQUARE_ROOT_FIFTH_POWER = 3,
	eTRANSFER_FUNC_SPECIAL_CURVE = 4,
	eTRANSFER_FUNC_SQUARE = 5,
	
	eTRANSFER_FUNC_DISCRETE = 230,
	eTRANSFER_FUNC_SQUARE_ROOT_PLUS_SPE_CURVE = 231,
	eTRANSFER_FUNC_SQUARE_ROOT_THIRD_POWER_PLUS_SPEC_CURVE = 232,
	eTRANSFER_FUNC_SQUARE_ROOT_FIFTH_POWER_PLUS_SPEC_CURVE = 233,
	eTRANSFER_FUNC_NONE = 251,
	eTRANSFER_FUNC_UNKNOWN = 252,
	eTRANSFER_FUNC_SPECIAL = 253,
}EnumTransferFuncCode;
// Table6.Alarm Selection Codes
typedef enum
{
	eALARM_SEL_HIGH = 0x00,
	eALARM_SEL_LOW = 1,
	eALARM_SEL_HOLD_LAST_OUT_VAL = 239,
	eALARM_SEL_MANUFAC_SPEC_DEF = 240, // 240-249
	eALARM_SEL_NONE = 251,
	eALARM_SEL_UNKNOWN = 252,
	eALARM_SEL_SPECIAL = 253,
}EnumAlarmSelCode;
// Table7.Write Protect Codes
typedef enum
{
	eWRITE_PRO_NOT_WRITE_PROTECTED = 0,
	eWRITE_PRO_WRITE_PROTECTED = 1,
	eWRITE_PRO_NONE = 251,
	eWRITE_PRO_UNKNOWN = 252,
	eWRITE_PRO_SPECIAL = 253,
}EnumWriteProtectCode;
// Table10.Physical Signaling Codes
typedef enum
{
	eBELL202_CURRENT = 0x00,
	eBELL202_VOLTAGE = 0x01,
	eRS485 = 0x02,
	eRS232 = 0x03,
	eSPECIAL = 0x06,
}EnumPhysicalSigCode;
// Table11.Flag Assignments
typedef enum
{
	eMULTI_SENSOR_FIELD_DEV = 0x01,
	eEEPROM_CTL = 0x02,
	ePROTOCAOL_BRIDGE_DEV = 0x04,
	eC8PSK_CAPABLE_FIELD_DEV = 0x40,
	eC8PSK_IN_MULTI_DROP_ONLY = 0x80,
}EnumFlagAssignments;
// Table16.Loop Current Mode Codes
typedef enum
{
	eLOOP_CURRENT_MODE_DISABLE = 0,
	eLOOP_CURRENT_MODE_ENABLE = 1,
	eLOOP_CURRENT_MODE_NOT_USED = 250,
	eLOOP_CURRENT_MODE_NONE = 251,
	eLOOP_CURRENT_MODE_UNKNOWN = 252,
	eLOOP_CURRENT_MODE_SPECIAL = 253,
}EnumLoopCurrentModeCode;
// Table17.Extend Device Status Codes
typedef enum
{
	eMAIN_REQUIRED = 0x01,
	eDEV_VAR_ALERT = 0x02,
}EnumExtendedDevStatusCodes;
// Table21.Device Variable Classification Codes
typedef enum
{
	eTB21DeviceVariableNotClassified = 0,
	eTB21Temperature = 64,
	eTB21Pressure = 65,
	eTB21VolumetricFlow = 66,
	eTB21Velocity = 67,
	eTB21Volume = 68,
	eTB21Length = 69,
	eTB21Time = 70,
	eTB21Current = 84,
}EnumDevVariableClsCode;
// Table26.Analog Channel Flags
typedef enum
{
	eANALOG_CHG = 0x01,
}EnumAnalogChgFlgs;
// Table57.Device Profile Code
typedef enum
{
	eHART_PROCESS_AUTO_DEV = 1,
	eHART_DISCRETE_DEV = 2,
	ePROCESS_AUTO_AND_DISCRETE = 3,
	eIO_SYSTEM = 4,
	eWIRELESS_HART_PROCESS_AUTO_DEV = 129,
	eWIRELESS_HART_DISCRETE_DEV = 130,
	eWIRELESS_HART_HYBRID = 131,
	eWIRELESS_HART_GATEWAY = 132,
	eWIRELESS_HART_PROCESS_ADAPTER = 141,
	eWIRELESS_HART_DISCRETE_ADAPTER = 142,
	eWIRELESS_HART_ENABLE_HANDHELD = 144,
}EnumDevProfileCode;

// Command-Specific Response Codes
// Command 0 Read Unique Identifier
enum Cmd0ResponseCodes
{
	eCMD0_RES_CODE_SUCCESS = 0x00,
};
// Command 1 Read Primary Variable
enum Cmd1ResponseCodes
{
	eCMD1_SUCCESS = 0,
	eCMD1_ERROR_DEVICE_SPEC_CMD_ERROR = 6,
	eCMD1_WARNING_UPDATE_FAILURE = 8,
	eCMD1_ERROR_ACCESS_RESTRICTED = 16,
};
// Command 2 Read Loop Current And Percent Of Range
enum Cmd2ResponseCodes
{
	eCMD2_SUCCESS = 0,
	eCMD2_ERROR_DEVICE_SPEC_CMD_ERROR = 6,
	eCMD2_WARNING_UPDATE_FAILURE = 8,
	eCMD2_ERROR_ACCESS_RESTRICTED = 16,
};
// Command 3 Read Dynamic Variables And Loop Current
enum Cmd3ResponseCodes
{
	eCMD3_SUCCESS = 0,
	eCMD3_ERROR_DEVICE_SPC_CMD_ERROR = 6,
	eCMD3_WARNING_UPDATE_FAILURE = 8,
	eCMD3_ERROR_ACCESS_RESTRICTED = 16,
};
// Command 6 Write Polling Address
enum Cmd6ResponseCodes
{
	eCMD6_SUCCESS = 0,
	eCMD6_ERROR_INVALID_POLL_ADDR_SEL = 2,
	eCMD6_ERROR_TOO_FEW_DATA_BYTES_RECEIVED = 5,
	eCMD6_ERROR_DEV_SPEC_CMD_ERROR = 6,
	eCMD6_ERROR_IN_WRITE_PROTECT_MODE = 7,
	eCMD6_ERROR_INVALID_MODE_SEL = 12,
	eCMD6_ERROR_ACCESS_RESTRICTED = 16,
	eCMD6_ERROR_BUSY = 32,
};
// Command 7 Read Loop Configuration
enum Cmd7ResponseCodes
{
	eCMD7_SUCCESS = 0,
	eCMD7_ERROR_ACCESS_RESTRICTED = 16,
};
// Command 8 Read Dynamic Variable Classifications
enum Cmd8ResponseCodes
{
	eCMD8Success = 0,
	eCMD8ErrorAccessRestricted = 16,
};
// Command 12 Read Message
enum Cmd12ResponseCodes
{
	eCMD12_SUCCESS = 0,
	eCMD12_ERROR_ACCESS_RESTRICTED = 16,
	eCMD12_ERROR_BUSY = 32,
};
// Command 14 Read Primary Variable Transducer Information
enum Cmd14ResponseCode
{
	eCMD14_SUCCESS = 0,
	eCMD14_ERROR_ACCESS_RESTRICTED = 16,
	eCMD14_ERROR_BUSY = 32,
};
// Command 15 Read Device Information
enum Cmd15ResponseCode
{
	eCMD15_SUCCESS = 0,
	eCMD15_ERROR_ACCESS_RESTRICTED = 16,
	eCMD15_ERROR_BUSY = 32,
};
// struct
// Universal Commands, Response Data Bytes
// Command 0 Read Unique Identifier
typedef struct
{
	uint8_t ucCmdByte0; // byte0
	uint8_t ucMiniNumOfPream; // byte3
	uint16_t uiExpandDevType; // byte1~2
	
	uint8_t ucHartMajorRev; // byte4
	uint8_t ucDevRevLevel; // byte5
	uint8_t ucSoftwareRev; // byte6
	uint8_t ucHardwareRevAndPhySigCode; // byte7
	
	uint32_t ulDeviceId; // byte9~11
	
	uint8_t ucBitsFlags; // byte8
	uint8_t ucMiniPreamRespMsg; // byte12
	uint8_t ucMaxDevVars; // byte13
	uint8_t ucBitsExtFieldDevStatus; // byte16
	
	uint16_t uiConfigChgCnt; // byte14~15
	uint16_t uiManufacturerIdCode; // byte17~18
	
	uint16_t uiPrivateLabDisCode; // byte19~20
	uint8_t  ucDevProfile; // byte21
	uint8_t ucReserved0;
}STHartReadUniqueIdentifierRes;
// Command #1#3 Variable Unit
typedef struct
{
	EnumEngineeringUnitCode ePVUnit;
	EnumEngineeringUnitCode eSVUnit;
	EnumEngineeringUnitCode eTVUnit;
	EnumEngineeringUnitCode eQVUnit;
}STHartVariableUnit;
// Command #2 Read Loop Current And Percent Of Range
typedef struct
{
	float fPVLoopCurrent; // mA
	float fPVPerOfRange;
}STReadLoopCurAndPerOfRge;
// Command #6#7
typedef struct
{
	uint8_t ucPollingAddr;
	EnumLoopCurrentModeCode eLoopCurMode;
	uint8_t ucReserved[2];
}STHartLoopConfig;
// Command #8 Read Dynamic Variable Classifications
typedef struct
{
	EnumDevVariableClsCode ucPVCls;
	EnumDevVariableClsCode ucSVCls;
	EnumDevVariableClsCode ucTVCls;
	EnumDevVariableClsCode ucQVCls;
}STDynamicVariableCls;
// Command #12 Device Message
typedef struct
{
	uint8_t ucMsg[24];
}STHartDeviceMsg;
// Command #13 Tag, Descriptor, Date
typedef struct
{
	uint8_t ucTag[6];
	uint8_t reserved[2];
	
	uint8_t ucDescriptor[12];
	
	uint8_t ucDate[3];
}STHartTagDescriptorDate;
// Command #14 Primary Variable Transducer Information
typedef struct
{
	uint32_t ulTransSN;
	
	EnumEngineeringUnitCode eTransLimAndMiniSpanUnitCode;
	uint8_t ucReserved[3];
	
	float fUpperTransLimit;
	float fLowerTransLimit;
	float fMiniSpan;
}STHartPrimaryVarTransInfo;
// Command #15 Device Information
typedef struct
{
	EnumAlarmSelCode ePVAlarmSelCode;
	EnumTransferFuncCode ePVTransFunCode;
	EnumEngineeringUnitCode ePVUpLowRangeValUnitCode;
	uint8_t reserved0;
	
	float fPVUpperRangeVal;
	float fPVLowerRangeVal;
	float fPVDampingVal;
	
	EnumWriteProtectCode eWriteProtectCode;
	uint8_t ucReserved;
	uint8_t ucPVAnalogChgFlgs;
	uint8_t reserved1;
}STDeviceInfo;
/* USER CODE END ET */

/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */
#define HART_CMD0_RESPONSE_DATA_SIZE                                        (22)
/* USER CODE END EC */

/* Exported variables ------------------------------------------------------------*/
/* USER CODE BEGIN EM */
extern STHartReadUniqueIdentifierRes stReadUniqueIdRes;
extern STHartVariableUnit stVariablesUnit;
extern STHartLoopConfig stLoopConfig;
extern STDynamicVariableCls stDynamicVariableCls;
extern STHartDeviceMsg stDeviceMsg;
extern STHartTagDescriptorDate stHartTagDescriptorDate;
extern STHartPrimaryVarTransInfo stPVTransducerInfo;
extern STDeviceInfo stDeviceInfo;
/* USER CODE END EM */

/* Exported functions prototypes ---------------------------------------------*/
/* USER CODE BEGIN EFP */
void vExeCommands(EnumHartCmdId eCmdID, uint8_t argc, uint8_t *argv);
uint8_t ucGetCRC(uint8_t *pData, uint8_t size);

extern float gfPVValue;
/* USER CODE END EFP */

/* Private defines -----------------------------------------------------------*/
/* USER CODE BEGIN Private defines */

/* USER CODE END Private defines */
#endif // _HARTCMD0_H_
